!(function () {
    var i18nStore = {};

    var i18nFormatters = {};

    BI._.extend(BI, {
        changeI18n: function (i18n) {
            if (i18n) {
                i18nStore = i18n;
            }
        },
        addI18n: function (i18n) {
            BI.extend(i18nStore, i18n);
        },

        i18nText: function (key) {
            var localeText = i18nStore[key] || (BI.i18n && BI.i18n[key]) || "";
            if (!localeText) {
                localeText = key;
            }
            var len = arguments.length;
            if (len > 1) {
                if (localeText.indexOf("{R1") > -1) {
                    for (var i = 1; i < len; i++) {
                        var reg = new RegExp(`{R${i},(.*?)}`, "g");

                        var result = reg.exec(localeText);

                        if (result) {
                            var formatName = result[1];
                            localeText = BI.replaceAll(localeText, reg, i18nFormatters[formatName](key, arguments[i]));
                        } else {
                            localeText = BI.replaceAll(localeText, `{R${i}}`, arguments[i] + "");
                        }
                    }
                } else {
                    var args = Array.prototype.slice.call(arguments);
                    var count = 1;
                    return BI.replaceAll(localeText, "\\{\\s*\\}", function () {
                        return args[count++] + "";
                    });
                }
            }
            return localeText;
        },

        addI18nFormatter: function (formatName, fn) {
            i18nFormatters[formatName] = fn;
        }
    });
})();
